package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.bean.UserCloseBean;
import com.koron.css2.clientCenter.bean.UserCloseDetailBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.CloseUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户销户增加
 *
 * @author 甄玉晴
 *
 */
public class CloseUserAdd implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Map.class);
		try {
			CloseUserMapper mapper = factory.getMapper(CloseUserMapper.class);
			UserCloseBean bean = JsonUtils.objectToPojo(req.getData(), UserCloseBean.class);
			if (bean.getList().size()==0) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "销户列表不能为空", void.class);
			}
			// 获取销户信息
			bean.setCreateInfo(userInfo);
			bean.setBillNo(Tools.getBillNo(factory, "USER_CLOSE"));
			bean.setBillDate(Tools.getCurrentDate());
			// 记录销户信息
			mapper.userCloseAdd(bean);

			//批量修改销户详情表和销户表关联
//			mapper.userCloseBatchUpdate(bean.getList(),bean.getId());
			//批量插入销户详情信息
			mapper.userCloseBatchAdd(bean.getList(),bean.getId());

			List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
			// 设置用户变更事项。只显示变更用户状态
			//记录每个用户的变更记录
			for (UserCloseDetailBean userCloseDetailBean: bean.getList()){
				UserChangeItemBean ubean = new UserChangeItemBean();
				ubean.setReceiptId(bean.getId());
				ubean.setReceiptType("USER_CLOSE");
				ubean.setBillNo(bean.getBillNo());
				ubean.setChangeItem("用户状态");
				ubean.setOldItemValue("1");
				ubean.setNewItemValue("0");
				ubean.setCreateInfo(userInfo);
				ubean.setUserNo(userCloseDetailBean.getUserNo());
				item.add(ubean);
			}
			ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
			cmapper.userChangeItemAdd(item, 2);

			// 返回ID
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", bean.getId());
			msg.setData(map);
//			BaseReceiptAccessoryMapper accessoryMapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
//			if (!StringUtils.isBlank(bean.getTempId())) // 修改附件ID
//				accessoryMapper.updateAccessoryReceiptId(bean.getId(), bean.getTempId());
			// 输出执行结果
			return msg;
		} catch (Exception e) {
			logger.error("销户增加失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "销户增加失败", void.class);
		}
	}

}
